package control;

import java.util.List;
import java.util.Random;

import javax.swing.JPanel;

import view.Map;

import model.general.Location;
import model.general.Util;
import model.move.IMove;
import model.move.MoveItems;
import model.objects.Boat;
import model.objects.Fish;
import model.objects.Hook;
import model.objects.IAnimateItems;
import model.objects.IAnimateMainActor;
import model.objects.IAnimateSubActor;
import model.observer.Obsever;
import model.observer.Obseverable;
import model.option.Option;
import model.option.OptionAddLife;
import model.option.OptionAddScore;
import model.option.OptionAddSpeed;

public class CreateFish extends Thread {
	private int QUATITY_ITEMS = 0;
	private int height, width;
	private IAnimateMainActor boat;
	private List<IAnimateItems> listItems;
	private JPanel pnl;
	private Obseverable observerable;
	private char kindItem;
	private int speed;
	private String imgleft = "";
	private String imgright = "";

	public CreateFish(int QUATITY_ITEMS, IAnimateMainActor boat,
			List<IAnimateItems> listItems, JPanel pnl, char kindItem, int speed) {
		super();
		this.pnl = pnl;
		this.QUATITY_ITEMS = QUATITY_ITEMS;
		this.height = pnl.getHeight();
		this.width = pnl.getWidth();
		this.boat = boat;
		this.listItems = listItems;
		this.kindItem = kindItem;
		this.speed = speed;
		observerable = this.boat.getObserable();

	}

	@Override
	public void run() {
		createFish();
		super.run();
	}

	private void createFish() {
		int count = 0;
		int w = width / 2;
		int h = height / 2;
		IAnimateItems f = null;
		Option option = null;
		Random rand = new Random();
		while (count < QUATITY_ITEMS) {
			if (Math.random() < 0.05) {

				// move = new MoveItems(location);
				int id = rand.nextInt(2);
				switch (kindItem) {
				case Util.ID_FISH_ITEM:
					option = new OptionAddScore((Boat) boat, 1);
					imgleft = "fish" + id + "-left.png";
					imgright = "fish" + id + "-right.png";
					break;
				case Util.ID_GOOD_ITEM_ADD_lIFE:
					option = new OptionAddSpeed((Boat) boat,
							Util.DEFAULT_SPEED_MOVE_BOAT + 20);
					imgleft = "heart" + 1 + ".png";
					imgright = imgleft;
					break;
				case Util.ID_BAD_ITEM_SUB_SCORE:
					option = new OptionAddScore((Boat) boat, -1);
					// imgleft = "fishsub"+4+"-left.png";
					imgright = "fishsub" + 4 + "-right.png";
					imgleft = imgright;
					break;
				}
				
				f = new Fish(randLocation(), imgleft, imgright, pnl, option,
						boat.getHook());
				f.setSpeed(speed);
				listItems.add(f);
				observerable.addObsever(f.getObserver());
				count++;

			}
			try {
				Thread.sleep(20);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	private Location randLocation() {
		Random rand = new Random();
		double m = Math.random();
		int x;
		if (m <= 0.5) {
			x = this.pnl.getX() - this.width - Util.LIMITED_LEFT_VIEW; // 0 - 48 = -48
		} else {
			x = this.pnl.getX() + this.pnl.getWidth() + Util.LIMITED_RIGHT_VIEW; // 0 + 800 = 800
		}
		// int y = rand.nextInt(this.pnl.getY() + this.pnl.getHeight()/2);
		// y += this.pnl.getHeight()/2;
		int h = this.pnl.getHeight() / 2;
		int y = h + rand.nextInt(h);
		return new Location(x, y);
	}

}
